﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace LogHelper
{
    public static class LogHelper
    {
        /// <summary>
        /// 日志锁
        /// </summary>
        private static object LockObj = new object();

        #region 路径
        /// <summary>
        /// 基础路径
        /// </summary>
        private static readonly string basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        /// <summary>
        /// 日志路径前缀
        /// </summary>
        private static readonly string pathHeard = "/";
        /// <summary>
        /// 日志记录路径
        /// </summary>
        private static readonly string LogPath = "/AppLog";
        #endregion

        public static void WriteLog(Exception ex, LogType type = LogType.Simple)
        {
            var fileName = EachTypePathCreateAndReturnFileName(type);
            StringBuilder message = new StringBuilder();
            message.Append($"[{DateTime.Now.ToString("yyyy-dd-MM HH:mm:ss")}]==>");
            message.Append($"异常位置：{ex.StackTrace.ToString()}{Environment.NewLine}");
            message.Append($"异常信息：{ex.Message.ToString()}");

            WriteLogFile(message.ToString(), fileName);
        }

        /// <summary>
        /// 日志工厂
        /// </summary>
        /// <param name="message"></param>
        /// <param name="type"></param>
        public static void WriteLog(string message, LogType type = LogType.Ordinary)
        {
            var fileName = EachTypePathCreateAndReturnFileName(type);

            WriteLogFile(message, fileName);
        }

        /// <summary>
        /// 根据类型建立路径，并返回文件名及具体路径
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static string EachTypePathCreateAndReturnFileName(LogType type)
        {
            CreateBasePath();
            StringBuilder path = new StringBuilder();
            path.Append(basePath);
            path.Append(LogPath);
            string fileName = string.Empty;
            var typePath = type.ToString();
            path.Append($"{pathHeard}{typePath}");
            fileName = $"{typePath.ToLower()}_{DateTime.Now.ToString("yyyyMMdd")}.log";
            PathIsCreate(path.ToString());

            return $"{path.ToString()}/{ fileName}";
        }

        /// <summary>
        /// 使用StreamWrite写入文件
        /// </summary>
        /// <param name="msg">信息</param>
        /// <param name="path">路径</param>
        private static void WriteLogFile(string msg, string path)
        {
            lock (LockObj)
            {
                using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
                {
                    sw.WriteLine($"{msg}{Environment.NewLine}");
                    sw.Flush();
                }
            }
        }

        /// <summary>
        /// 在软件基础目录建立AppLog文件夹
        /// </summary>
        private static void CreateBasePath()
        {
            PathIsCreate($"{basePath}{LogPath}");
        }

        private static void PathIsCreate(string path)
        {
            if (Directory.Exists(path))
                return;
            else
                Directory.CreateDirectory(path);
        }
    }

    public enum LogType
    {
        Ordinary,
        Simple,
        Error,
    }
}
